import { _decorator, Collider, Component, EventKeyboard, EventTouch, input, Input, ITriggerEvent, KeyCode, Node } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Load')
export class Load extends Component {
    @property(Node)
    private Player: Node = null;

    playerMoveSpeed: number = 100;

    leftMove: boolean = false;
    rightMove: boolean = false;
    protected onLoad(): void {

    }
    protected start(): void {

    }
    protected onEnable(): void {
        input.on(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.on(Input.EventType.KEY_UP, this.onKeyUp, this);
        //this.Player.getComponent(Collider).on("onTriggerEnter", this.onTriggerEnter, this);
    }
    protected onDisable(): void {
        input.off(Input.EventType.KEY_DOWN, this.onKeyDown, this);
        input.off(Input.EventType.KEY_UP, this.onKeyUp, this);
        //this.Player.getComponent(Collider).off("onTriggerEnter", this.onTriggerEnter, this);
    }
    protected update(dt: number): void {
        if (this.leftMove) {
            this.Player.setPosition(this.Player.position.x - this.playerMoveSpeed * dt, this.Player.position.y, this.Player.position.z);
        }
        if (this.rightMove) {
            this.Player.setPosition(this.Player.position.x + this.playerMoveSpeed * dt, this.Player.position.y, this.Player.position.z);
        }
    }
    private onKeyDown(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A: // A
                this.leftMove = true;
                break;
            case KeyCode.KEY_D: // D
                this.rightMove = true;
                break;
        }
    }
    private onKeyUp(event: EventKeyboard) {
        switch (event.keyCode) {
            case KeyCode.KEY_A: // A
                this.leftMove = false;
                break;
            case KeyCode.KEY_D: // D
                this.rightMove = false;
                break;
        }
    }
    private onTriggerEnter(event: ITriggerEvent) {
        console.log("onTriggerEnter" + event.selfCollider.node.name + "触发了" + event.otherCollider.node.name + "的触发器");
    }
}


